package com.sansan.qiangji.controller.system;

import com.sansan.qiangji.VO.system.power.PowerVO;
import com.sansan.qiangji.enums.ResultEnum;
import com.sansan.qiangji.exception.ServiceException;
import com.sansan.qiangji.form.system.power.PowerAddForm;
import com.sansan.qiangji.form.system.power.PowerEditForm;
import com.sansan.qiangji.form.system.power.PowerListForm;
import com.sansan.qiangji.realize.system.PowerReal;
import com.sansan.qiangji.resultVO.ResultVO;
import com.sansan.qiangji.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

@CrossOrigin()
@RestController
@RequestMapping("/power")
@Slf4j
public class PowerController {
    private PowerReal powerReal;

    @Resource
    public void setPowerReal(PowerReal powerReal){
        this.powerReal = powerReal;
    }

    @PostMapping("/create")
    public ResultVO<Object> createPower(@Valid @RequestBody PowerAddForm powerAddForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【创建权限】参数不正确，powerAddForm={}", powerAddForm.toString());
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        powerReal.createPower(powerAddForm);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/list")
    public ResultVO<Object> list(@Valid @RequestBody PowerListForm powerListForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【权限列表】参数不正确，userListForm={}", powerListForm.toString());
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        return ResultVOUtil.success(powerReal.getPowers(powerListForm));
    }

    @PostMapping("/rolePowerList")
    public ResultVO<Object> rolePowerList(@RequestParam(name = "roleId") Integer roleId) {
        return ResultVOUtil.success(powerReal.rolePowerList(roleId));
    }

    @PostMapping("/userPowerList")
    public ResultVO<Object> userPowerList(@RequestParam(name = "userId") Integer userId) {
        return ResultVOUtil.success(powerReal.userPowerList(userId));
    }

    @PostMapping("/delete")
    public ResultVO<Object> deletePower  (@RequestParam(name = "powerId") Integer powerId) {
        powerReal.deletePower(powerId);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/changeStatus")
    public ResultVO<Object> changePowerStatus(@RequestParam(name = "powerId") Integer powerId) {
        powerReal.changePowerStatus(powerId);
        return ResultVOUtil.success(null);
    }

    @PostMapping("/edit")
    public ResultVO<Object> editMenu(@Valid @RequestBody PowerEditForm powerEditForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            log.error("【权限列表】参数不正确，powerEditForm={}", powerEditForm.toString());
            throw new ServiceException(ResultEnum.PARAM_ERROR.getCode(), Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
        }
        PowerVO powerVO = powerReal.editPower(powerEditForm);
        return ResultVOUtil.success(powerVO);
    }
}
